const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight+'px',
    customBar:app.globalData.customBar+'px',
    navheight:app.globalData.customBar-app.globalData.statusBarHeight+'px',
    listpic:[],
    AnswerInfopic:[],
    AnswerInfo:[],
    tqid:19,
    info:[],
    footshow:true,
    huida:false,
    piclist:[],
    true:true,
    false:false,
    keyHeight:'275px',
    piclist:[],
    Messages:'',  
    intCurrPage:1,
    pxtype:'default',  
    intRecordCount:0,
    hdlist:[],
    xglist:[],
    rmlist:[],
    tabbarshow:false,
    isHuifu:false,
    huifuplaceholder:'',
    huifucanshu:[],
    TAID:9,
    PPID:0,
    PingLunList:[],
    AnswerNextID:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      TAID:options.taid
    })
    // this.getFeijiu_Get_QuestionInfo();
    this.getFeijiu_Get_AnswerInfo();
  },
  bindTextAreaDesc:function (e) {
    this.setData({
      Messages:e.detail.value,
      keyHeight:'0px'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.onKeyboardHeightChange(res => {      
    //   that.setData({
    //     keyHeight:res.height+'px'
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function( options ){
  　　var that = this;  　　
  　　var shareObj = {
  　　　　title: that.data.info.Title,      
  　　　　path: '/pages/wenda/detaile?taid='+this.data.TAID,    
  　　　　imgUrl: '',
  　　};  　
  　　if( options.from == 'button' ){
  　　　　shareObj.path = '/pages/wenda/detaile?taid='+this.data.TAID;
  　　}  　　
  　　return shareObj;
  },  
  toPreviewImage:function (e) {
    var that = this;
    var current = e.currentTarget.dataset.url;
      wx.previewImage({
        current: current, 
        urls: that.data.listpic
      })
  },
  toback:function () {
    wx.navigateBack()
  },
  // getFeijiu_Get_QuestionInfo:function () {
  //   var that = this;
  //   var api_input = {
  //     tqid:this.data.tqid
  //   }
  //   app.getDaoPostN('api/FeijiuApp/Feijiu_Get_QuestionInfo',api_input,function (r) {
  //     console.log(r)
  //     if (r.code == 200) {
  //       var info = r.data.Info;
  //       info.AddTime = app.dateformatT(info.AddTime,10)
  //       if (info.Photo.length > 0) {
  //         that.setData({
  //           listpic:info.Photo.split('|')
  //         })
  //       }
  //       that.setData({
  //         info:info,
  //         xglist:r.data.XgData,
  //         rmlist:r.data.RmData,
  //       })
  //     }
  //   })     
  // },
  tohuida:function () {    
    this.setData({
      footshow:false,
      huida:true
    })
  },
  onFocus:function (e) {
    this.setData({
      keyHeight:e.detail.height+'px'
    })
  },
  noaction:function () {
    // body...
  },
  hidehuida:function () {
    this.setData({
      footshow:true,
      huida:false,
      isHuifu:false,
    })
  },
  tohuifu:function (e) {
    this.setData({
      footshow:false,
      isHuifu:true,
      PPID:e.currentTarget.dataset.ppid,
      huifuplaceholder:'正在回复 '+e.currentTarget.dataset.name
    })
  },
  huifuBtn:function () {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });  return false;
    } 
    if (this.data.Messages.length < 5 ) {
      app.tishi('回复内容不能少于5个字!');return false;
    }
    var api_input = {
      TQID:this.data.tqid,
      TAID:this.data.TAID,
      PPID:this.data.PPID,
      Content:this.data.Messages,
      UID:FjLoginLog.UID,
      Uname:FjLoginLog.UName,
      HeadPic:''

    }
   
    app.getDaoPostN('api/FeijiuApp/Feijiu_ADD_APingLuna',api_input,function (r) {      
      if (r.code == 200) {
        app.tishi('回复成功!审核后可显示');
        that.setData({
          footshow:true,
          isHuifu:false
        })
      }else{
        app.tishi('回复失败');
      }
    })
  },


  fabuhuidaBtn:function () {    
    var that = this;

    var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });  return false;
    } 

    if (this.data.Messages.length < 5 ) {
      app.tishi('回答内容不能少于5个字!');return false;
    }
    var api_input = {
      TQID:this.data.tqid,
      Photo:this.data.piclist.join('|'),
      Messages:this.data.Messages,
      UID:FjLoginLog.UID,
      Uname:FjLoginLog.UName,
      HeadPic:'',
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_ADD_Answer',api_input,function (r) {      
      if (r.code == 200) {
        app.tishi('回答问题成功!审核后可显示');
        that.setData({
          footshow:true,
          huida:false
        })
      }else{
        app.tishi('回答问题失败');
      }
    })
  },
  uploadPic:function (e) {
    var that = this;
    wx.chooseImage({
      count:1,
      success (res) {
        const tempFilePaths = res.tempFilePaths;
        var api_input = {
           imgType: 22
        }
        var arr = app.creatSecretStr('Feijiu_UpyUpload_Img',JSON.stringify(api_input));
        wx.showLoading({
          title: "图片上传中"       
        }) 
        wx.uploadFile({
          url: app.data.api_url+'api/FeijiuApp/Feijiu_UpyUpload_Img',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: arr,
          success (res){
            var data = JSON.parse(res.data);
            if (data.code == 200) {
              var list = that.data.piclist;
              list.push(data.data); 
              that.setData({
                piclist:list
              })             
            }
          },
          complete(res){
            wx.hideLoading();
          }
        })
      }
    }) 
  },
  delpic:function (e) {
    var idx = e.currentTarget.dataset.idx;
    var list = this.data.piclist;
    list.splice(idx, 1);
    this.setData({
      piclist:list
    })
  },
  woyaotiwen:function () {
    if (app.checkFjLogin()) {
      wx.navigateTo({
        url: 'woyaotiwen'
      })
    }else{
      wx.navigateTo({
        url: '../login/index'
      })      
    }
  },
  //获取回答列表
  getFeijiu_Get_AnswerInfo:function () {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
    if (app.checkFjLogin()) {
      var api_input = {
        TAID:this.data.TAID,
        UID:FjLoginLog.UID
      } 
    }else{
      var api_input = {
        TAID:this.data.TAID,
        UID:0
      } 
    }   
    app.getDaoPostN('api/FeijiuApp/Feijiu_Get_AnswerInfo',api_input,function (r) {
      if (r.code == 200) {
       
        var info = r.data.QuestionInfo;
        info.AddTime = app.dateformatT(info.AddTime,10);
        if (info.Photo.length > 0) {
          that.setData({
            listpic:info.Photo.split('|')
          })
        }
        that.setData({
          info:info,
          xglist:r.data.XgData,
          rmlist:r.data.RmData,
          PingLunList:r.data.PingLunList,
          AnswerNextID:r.data.AnswerNextID
        })   
        var AnswerInfo = r.data.AnswerInfo;
        AnswerInfo.AddTime = app.dateformatT(AnswerInfo.AddTime,10);
        AnswerInfo.IsZan = r.data.IsZan;
        if (AnswerInfo.Photo.length > 0) {
          that.setData({
            AnswerInfopic:AnswerInfo.Photo.split('|')
          })
        }        
        that.setData({
          AnswerInfo:AnswerInfo
        })
      }      
    })    
  },
  todetaile:function (e) {
    var tqid = e.currentTarget.dataset.tqid;
    wx.navigateTo({
      url: 'detaile?tqid='+tqid
    })
  }, 
  //监测屏幕滚动 
  onPageScroll:function (e) {
    if (e.scrollTop > 120) {
      this.setData({
        tabbarshow:true
      })
    }else{
      this.setData({
        tabbarshow:false
      })      
    }
  },
  //点赞
  dianzan:function (e) {
    //先判断是否要登录
    if (app.checkFjLogin()) {

      if (e.currentTarget.dataset.ztype == 1) {
        if (this.data.AnswerInfo.IsZan == 1) {
          app.tishi('您已经赞过该条信息了!');return false;
        }
      }else{
        if (this.data.PingLunList[e.currentTarget.dataset.idx].iszan == 1) {
          app.tishi('您已经赞过该条信息了!');return false;
        }
      }

      var that =this;
      var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
      var api_input = {
        ztype:e.currentTarget.dataset.ztype,
        InfoID:e.currentTarget.dataset.infoid,
        UID:FjLoginLog.UID
      }      
      app.getDaoPostN('api/FeijiuApp/Feijiu_ADD_AQZan',api_input,function (r) {
        if (r.code == 200) {
          app.tishi(r.data)
          if (e.currentTarget.dataset.ztype == 1) {
            var iszan = 'AnswerInfo.IsZan';
            var zan = 'AnswerInfo.Zan';
            var num = that.data.AnswerInfo.Zan;
          }else{
            var iszan = 'PingLunList['+e.currentTarget.dataset.idx+'].iszan';
            var zan = 'PingLunList['+e.currentTarget.dataset.idx+'].zan';
            var num = that.data.PingLunList[e.currentTarget.dataset.idx].zan;
          }
          that.setData({
            [iszan]:1,
            [zan]:num+1
          })
        }      
      })  
    }else{
      wx.navigateTo({
        url: '../login/index'
      })      
    }
  },
  //下一个评论
  toNextPl:function () {    
    this.setData({
      TAID:this.data.AnswerNextID
    })
    
    this.getFeijiu_Get_AnswerInfo();
    wx.pageScrollTo({
        scrollTop: 0
    })    
  }

})